/****************************************************************************
 * 2025.8 枯木逢春
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Linq;

namespace Projectsurviva
{
    /// <summary>
    /// 显示层
    /// </summary>
    public partial class CoinupdataPanel : UIElement, IController
    {
        //非常暴力
     
        private void Awake()
        {
            Coinupdatatprefab.Hide();

            Coinupdataitemgroup.DestroyChildren();

            foreach (var item in this.GetSystem<CoinupdataSystem>().Items.Where(item => item.ConditionCheck()))
            {
                Coinupdatatprefab.InstantiateWithParent(Coinupdataitemgroup).Self(self =>
                {
                    var itemcache = item;
                    self.GetComponentInChildren<Text>().text = item.Description;

                    self.onClick.AddListener(() =>
                    {
                        item.Upgrade();
                        AudioKit.PlaySound("_ability_level_up");
                    });
                    var selfcache = self;
                    item.OnChanged.Register(() =>
                    {
                        if (itemcache.ConditionCheck())
                        {
                            selfcache.Show();
                        }
                        else
                        {
                            selfcache.Hide();
                        }
                    }).UnRegisterWhenGameObjectDestroyed(selfcache);
                    if (itemcache.ConditionCheck())
                    {
                        selfcache.Show();
                    }
                    else
                    {
                        selfcache.Hide();
                    }
                    Global.Coin.RegisterWithInitValue(coin =>
                    {
                        cointxt.text = "金币" + coin;
                        if (coin > itemcache.price)
                        {
                            selfcache.interactable = true;
                        }
                        else
                        {
                            selfcache.interactable = false;

                        }

                    }).UnRegisterWhenGameObjectDestroyed(self);
                });
            }

            btnclose.onClick.AddListener(() =>
   {
       this.Hide();
   });
        }

        protected override void OnBeforeDestroy()
        {
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;

        }
      
    }
}